import tkinter as tk
from tkinter import filedialog, messagebox, ttk
import threading
import os
import process_data

class App:
    def __init__(self, root):
        self.root = root
        self.root.title('Easy DataSet标签增强工具')
        self.center_window(600, 420)
        self.dict_file = ''
        self.csv_file = ''
        self.output_file = ''
        self.create_widgets()

    def center_window(self, w, h):
        ws = self.root.winfo_screenwidth()
        hs = self.root.winfo_screenheight()
        x = (ws // 2) - (w // 2)
        y = (hs // 2) - (h // 2)
        self.root.geometry(f'{w}x{h}+{x}+{y}')

    def create_widgets(self):
        title = tk.Label(self.root, text='Easy DataSet标签增强工具', font=('微软雅黑', 18, 'bold'), fg='#3366cc')
        title.grid(row=0, column=0, columnspan=3, pady=(10, 5))
        # 版权信息紧跟标题下方
        copyright_label = tk.Label(self.root, text='© Sohook® 搜获客', font=('微软雅黑', 10, 'bold'), fg='#444444', anchor='center')
        copyright_label.grid(row=1, column=0, columnspan=3, pady=(0, 10), sticky='we')

        # 字典文件选择
        tk.Label(self.root, text='字典文件:', font=('微软雅黑', 11)).grid(row=2, column=0, sticky='e', padx=5)
        self.dict_entry = tk.Entry(self.root, width=40, font=('微软雅黑', 10))
        self.dict_entry.grid(row=2, column=1)
        tk.Button(self.root, text='选择...', command=self.choose_dict, bg='#e6f2ff').grid(row=2, column=2, padx=5)

        # CSV文件选择
        tk.Label(self.root, text='CSV文件:', font=('微软雅黑', 11)).grid(row=3, column=0, sticky='e', padx=5)
        self.csv_entry = tk.Entry(self.root, width=40, font=('微软雅黑', 10))
        self.csv_entry.grid(row=3, column=1)
        tk.Button(self.root, text='选择...', command=self.choose_csv, bg='#e6f2ff').grid(row=3, column=2, padx=5)

        # 输出文件名
        tk.Label(self.root, text='输出文件名:', font=('微软雅黑', 11)).grid(row=4, column=0, sticky='e', padx=5)
        self.output_entry = tk.Entry(self.root, width=40, font=('微软雅黑', 10))
        self.output_entry.grid(row=4, column=1)
        self.output_entry.insert(0, 'output.csv')

        # 处理按钮
        self.run_btn = tk.Button(self.root, text='开始处理', command=self.run, bg='#99cc99', font=('微软雅黑', 12, 'bold'))
        self.run_btn.grid(row=4, column=2, pady=10, padx=5)

        # 进度条
        self.progress = ttk.Progressbar(self.root, orient='horizontal', length=500, mode='indeterminate')
        self.progress.grid(row=5, column=0, columnspan=3, pady=(5, 0))

        # 日志输出
        tk.Label(self.root, text='日志输出:', font=('微软雅黑', 11)).grid(row=6, column=0, sticky='nw', padx=5)
        self.log_text = tk.Text(self.root, width=70, height=13, state='disabled', font=('Consolas', 10), bg='#f8f8ff')
        self.log_text.grid(row=7, column=0, columnspan=3, padx=10, pady=5)

    def choose_dict(self):
        file = filedialog.askopenfilename(title='选择字典文件', filetypes=[('Text Files', '*.txt'), ('All Files', '*.*')])
        if file:
            self.dict_entry.delete(0, tk.END)
            self.dict_entry.insert(0, file)

    def choose_csv(self):
        file = filedialog.askopenfilename(title='选择CSV文件', filetypes=[('CSV Files', '*.csv'), ('All Files', '*.*')])
        if file:
            self.csv_entry.delete(0, tk.END)
            self.csv_entry.insert(0, file)

    def run(self):
        self.run_btn.config(state='disabled')
        self.progress.start(10)
        self.log_text.config(state='normal')
        self.log_text.delete(1.0, tk.END)
        self.log_text.insert(tk.END, '开始处理...\n')
        self.log_text.config(state='disabled')
        threading.Thread(target=self._run_process).start()

    def _run_process(self):
        dict_file = self.dict_entry.get().strip()
        csv_file = self.csv_entry.get().strip()
        output_file = self.output_entry.get().strip()
        if not dict_file or not csv_file or not output_file:
            self.append_log('请完整选择字典文件、CSV文件和输出文件名！')
            self.run_btn.config(state='normal')
            self.progress.stop()
            return
        try:
            process_data.run_process(dict_file, csv_file, output_file)
            self.append_log(f'处理完成，结果已保存到 {output_file}')
        except Exception as e:
            self.append_log(f'处理出错: {str(e)}')
        self.run_btn.config(state='normal')
        self.progress.stop()

    def append_log(self, msg):
        self.log_text.config(state='normal')
        self.log_text.insert(tk.END, msg + '\n')
        self.log_text.see(tk.END)
        self.log_text.config(state='disabled')

if __name__ == '__main__':
    root = tk.Tk()
    app = App(root)
    root.mainloop() 